package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.CategoryEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class BusinessErrorActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private EditText businessNameEt;
	private LinearLayout areaLayout;
	private TextView areaTv;
	private LinearLayout categoryLayout;
	private TextView categoryTv;
	private EditText businessAddressEt;
	private EditText businessNearbyEt;
	private EditText businessTelEt;
	private LinearLayout rightLayout;

	private RadioGroup categoryFilterLeftLayout;
	private FrameLayout categoryFilterRightLayout;
	/****************** 数据变量 **************************/
	private BusinessEntity businessInfo;
	private String businessNameStr;
	private String businessAreaStr;
	private String businessClassStr;
	private String businessAddressStr;
	private String businessNearbyStr;
	private String businessTelStr;
	private Map<String, Object> resultMap;
	// 总分类长度
	private int classLen;
	private int parentClassIndex;
	private List<CategoryEntity> categoryList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_error);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.businessNameEt = (EditText) findViewById(R.id.et_add_business_name);
		this.businessAddressEt = (EditText) findViewById(R.id.et_add_business_address);
		this.businessNearbyEt = (EditText) findViewById(R.id.et_add_business_nearby);
		this.businessTelEt = (EditText) findViewById(R.id.et_add_business_tel);
		this.areaLayout = (LinearLayout) findViewById(R.id.ll_business_error_area);
		this.categoryLayout = (LinearLayout) findViewById(R.id.ll_business_error_class);
		this.areaTv = (TextView) findViewById(R.id.tv_business_error_area);
		this.categoryTv = (TextView) findViewById(R.id.tv_business_error_class);
		this.rightLayout = (LinearLayout) findViewById(R.id.ll_common_right);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener errorClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_business_error_area:
					showAreaDialog(areaList, areaTv);
					break;
				case R.id.ll_business_error_class:
					showCategoryDialog(categoryTv);
					break;
				case R.id.ll_common_right:
					businessNameStr = businessNameEt.getText().toString()
							.trim();
					businessAreaStr = areaTv.getText().toString();
					businessClassStr = categoryTv.getText().toString();
					businessAddressStr = businessAddressEt.getText().toString()
							.trim();
					businessNearbyStr = businessNearbyEt.getText().toString()
							.trim();
					businessTelStr = businessTelEt.getText().toString().trim();
					if (verifyData()) {
						requestSendErrorData();
					}
					break;
				default:
					break;
				}
			}
		};
		this.areaLayout.setOnClickListener(errorClick);
		this.categoryLayout.setOnClickListener(errorClick);
		this.rightLayout.setOnClickListener(errorClick);
	}

	private boolean verifyData() {
		if (StringUtil.isNullOrEmpty(businessNameStr)) {
			showShortToast("请输入商家名称");
			return false;
		}
		if (StringUtil.isNullOrEmpty(businessAreaStr)) {
			showShortToast("请选择区域");
			return false;
		}
		if (StringUtil.isNullOrEmpty(businessClassStr)) {
			showShortToast("请选择分类");
			return false;
		}
		if (StringUtil.isNullOrEmpty(businessAddressStr)) {
			showShortToast("请输入商家地址");
			return false;
		}
		if (StringUtil.isNullOrEmpty(businessNearbyStr)) {
			showShortToast("请输入商家靠近地点");
			return false;
		}
		if (StringUtil.isNullOrEmpty(businessTelStr)) {
			showShortToast("请输入商家电话");
			return false;
		}
		return true;
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		businessInfo = (BusinessEntity) intent
				.getSerializableExtra("businessInfo");
	}

	/**
	 * 报告商户错误数据
	 */
	private void requestSendErrorData() {
		if (commonUserInfo == null) {
			showLoginDialog();
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
						businessInfoParamMap.put("sellerId",
								businessInfo.getSellerId());
						businessInfoParamMap.put("userId",
								commonUserInfo.getUserId());
						StringBuffer content = new StringBuffer();
						content.append("sellerName=" + businessNameStr + "@");
						content.append("sellerArea=" + commonNowCityName + "-"
								+ businessAreaStr + "@");
						content.append("sellerClass=" + businessClassStr + "@");
						content.append("sellerAddress=" + businessAddressStr
								+ "@");
						content.append("sellerNear=" + businessNearbyStr + "@");
						content.append("sellerTel=" + businessTelStr);
						businessInfoParamMap.put("content", content.toString());
						businessInfoParamMap.put("type", "3");
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								BusinessErrorActivity.this,
								Constants.SELLER_ERROR_API,
								businessInfoParamMap);
						if (commentItemList != null) {
						}
					}

					@Override
					public void loadEnd() throws Exception {
						showShortToast("保存成功");
						finish();
					}

				});
		ald.execute(0);
	}

	private Dialog dialog;

	public void showCategoryDialog(final TextView resultTv) {
		View view = LayoutInflater.from(this).inflate(R.layout.category, null);
		dialog = new Dialog(BusinessErrorActivity.this, R.style.listDialog);
		initCategory(view, resultTv);
		dialog.setContentView(view); // 设置显示用户信息列表的对话框视图
		dialog.show();
	}

	// TODO 初始化分类
	private void initCategory(View v, final TextView resultTv) {
		this.categoryFilterLeftLayout = (RadioGroup) v
				.findViewById(R.id.ll_conditon_filter_category_left);
		this.categoryFilterRightLayout = (FrameLayout) v
				.findViewById(R.id.ll_conditon_filter_category_right);

		LayoutInflater inflater = LayoutInflater.from(this);
		categoryList = Constants.getCategoryList();
		classLen = categoryList.size();
		View.OnClickListener leftClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				parentClassIndex = index;
				for (int i = 0, len = classLen; i < len; i++) {
					categoryFilterRightLayout.getChildAt(i).setVisibility(8);
				}
				categoryFilterRightLayout.getChildAt(index).setVisibility(0);
			}
		};
		View.OnClickListener rightClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				String parentClassName = categoryList.get(parentClassIndex)
						.getParentClassName();
				String childClassName = categoryList.get(parentClassIndex)
						.getChildClassList().get(index);
				resultTv.setText(parentClassName + "-" + childClassName);
				dialog.dismiss();
			}
		};
		for (int i = 0, len = categoryList.size(); i < len; i++) {
			View leftView = inflater.inflate(R.layout.category_left_item, null);
			RadioButton leftRB = (RadioButton) leftView;
			String parentCN = categoryList.get(i).getParentClassName();
			leftRB.setText(parentCN);
			leftRB.setId(i);
			if ("热门分类".equals(parentCN)) {
				leftRB.setChecked(true);
			}
			// 设置drawableleft
			BitmapDrawable leftbd = getListIco(i);
			leftRB.setCompoundDrawablesWithIntrinsicBounds(leftbd, null, null,
					null);
			// 设置tag
			leftRB.setTag(i);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			leftView.setOnClickListener(leftClick);
			this.categoryFilterLeftLayout.addView(leftView, lp);
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT));
			ll.setOrientation(LinearLayout.VERTICAL);
			ll.setVisibility(i == 0 ? 0 : 8);
			List<String> childClassList = categoryList.get(i)
					.getChildClassList();
			for (int j = 0, childLen = childClassList.size(); j < childLen; j++) {
				View rightView = inflater.inflate(R.layout.category_right_item,
						null);
				rightView.setBackgroundResource(R.drawable.ll_bg_selector);
				rightView.setTag(j);
				rightView.setOnClickListener(rightClick);
				TextView rightTv = (TextView) rightView
						.findViewById(R.id.tv_category_right);
				rightTv.setText(childClassList.get(j));

				ll.addView(rightView, lp);
			}
			this.categoryFilterRightLayout.addView(ll);
		}
	}

	private BitmapDrawable getListIco(int index) {
		Resources res = getResources();
		BitmapDrawable bd = null;
		Bitmap bm = null;
		switch (index) {
		case 0:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico1);
			bd = new BitmapDrawable(res, bm);
			break;
		case 1:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico2);
			bd = new BitmapDrawable(res, bm);
			break;
		case 2:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico3);
			bd = new BitmapDrawable(res, bm);
			break;
		case 3:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico4);
			bd = new BitmapDrawable(res, bm);
			break;
		case 4:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico5);
			bd = new BitmapDrawable(res, bm);
			break;
		case 5:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico6);
			bd = new BitmapDrawable(res, bm);
			break;
		case 6:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico7);
			bd = new BitmapDrawable(res, bm);
			break;
		case 7:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico8);
			bd = new BitmapDrawable(res, bm);
			break;

		default:
			break;
		}
		return bd;
	}
}